// pages/rent/orderInfo.js
const app = getApp();

Page({
  data: {
    haveCreateCollection: false,
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '公寓订单',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333",
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    images: [],
    text: '',
    info: {},
    typeId: 1,
    types: [ {name: "按天计", value: 0}, {name: "按月计", value: 1},],
    startDate: "",
    endDate: "",
    num: 0,
    minDate: "2022-10-10",
    maxDate: "2099-12-30",
    name: "",
    phone: "",
    idcard: "",
    remark: "",
    bill: [],
    bill_log: [],
    images: [],
    enableRefund: false,
    ready: false
  },
  onShow(){
    let v = this;
    const info = app.globalData.orderInfo;
    const newData = {};
    newData.info = info;
    if (info.type === 0) {
      newData.typeId = 0;
    }
    console.log("orderInfo", info);
    v.setData(newData);
    v.getDetail(info);
  },
  getDetail(info) {
    const v = this;
    const rent_id = info.rent_id;
    let url = app.globalData.host + '/api/rent/detail';
    wx.request({
      url: url, data: {token: app.globalData.token, rent_id: rent_id},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("getDetail", r);
          const newInfo = r.data;
          const bill = newInfo.bill || [];
          const bill_log = newInfo.bill_log || [];
          let enableRefund = false;
          if (newInfo.start_time) {
            let sdt = new Date(newInfo.start_time);
            let his = sdt.getTime() + 18 * 60 * 1000;
            let cdt = Date.now();
            if (cdt < his && newInfo.status == 1) enableRefund = true;
            console.log("enableRefund  cdt his" , cdt, his, enableRefund);
          }
          enableRefund = newInfo.can_refund || false;
          let images = [];
          if (newInfo.cert) {
            let l = newInfo.cert.split(',');
            for (let k in l) images.push({url: l[k]});
          }
          console.log("images",images);
          let newData = {info: newInfo, bill: bill, bill_log: bill_log, enableRefund: enableRefund, images: images};
          v.setData(newData);
        }
      }
    })
  },
  doPayBefore() {
    const v = this;
    let url = app.globalData.host + '/api/rent/payinfo';
    let rent_id = v.data.info.rent_id;
    wx.request({
      url: url, data: {token: app.globalData.token, rent_id: rent_id},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("doPayBefore", r);
          const info = r.data.payinfo;
          wx.requestPayment({
            timeStamp: info.timeStamp,
            nonceStr: info.nonceStr,
            package: info.package,
            signType: info.signType,
            paySign: info.paySign,
            success (res) { 
              wx.showToast({title: '支付成功', icon: 'none' });
              v.getDetail({rent_id: rent_id});
            },
            fail (res) {
              wx.showToast({ title: '支付失败', icon: 'none' });
             }
          })
        }
      }
    })
  },
  doApplyCancel() {
    const v = this;
    wx.showModal({
      title: '取消预约',
      content: '确定要取消此次预约吗？',
      success (res) {
        if (res.confirm) {
          v.doApplyCancelSubmit();
          console.log('取消预约 用户点击确定');
        } else if (res.cancel) {
          console.log('取消预约 用户点击取消');
        }
      }
    })
  },
  doApplyCancelSubmit() {
    const v = this;
    let url = app.globalData.host + '/api/rent/close';
    let rent_id = v.data.info.rent_id;
    wx.request({
      url: url, data: {token: app.globalData.token, rent_id: rent_id},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("doApplyCancel", r);
          if (r.msg) {
            wx.showToast({title: r.msg, icon: 'none' });
          } else {
            wx.showToast({title: '申请成功', icon: 'none' });
          }
          
          v.getDetail({rent_id: rent_id});
        }
      }
    })
  },
  doApplyRefund() {
    const v = this;
    console.log("doApplyRefund")
    let url = app.globalData.host + '/api/rent/check_out';
    let rent_id = v.data.info.rent_id;
    wx.request({
      url: url, data: {token: app.globalData.token, rent_id: rent_id},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("doApplyRefund", r);
          if (r.msg) {
            wx.showToast({title: r.msg, icon: 'none' });
          } else {
            wx.showToast({title: '申请成功', icon: 'none' });
          }
          v.getDetail({rent_id: rent_id});
        }
      }
    })
  },
  doPayOther() {
    const v = this;
    console.log("doPayOther 支付水电费用")
    let url = app.globalData.host + '/api/rent/payinfo';
    let rent_id = v.data.info.rent_id;
    wx.request({
      url: url, data: {token: app.globalData.token, rent_id: rent_id},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("doPayOther", r);
          const info = r.data.payinfo;
          wx.requestPayment({
            timeStamp: info.timeStamp,
            nonceStr: info.nonceStr,
            package: info.package,
            signType: info.signType,
            paySign: info.paySign,
            success (res) { 
              wx.showToast({title: '支付成功', icon: 'none' });
              v.getDetail({rent_id: rent_id});
            },
            fail (res) {
              wx.showToast({ title: '支付失败', icon: 'none' });
             }
          })
        }
      }
    })
  },
  doApplyTicketRent() {
    const v = this;
    const info = v.data.info;
    console.log("doApplyTicketRent", info);

    app.globalData.rent_id = info.rent_id;
    app.globalData.orderInfo = info;
    wx.navigateTo({ url: "/pages/rent/applyTicket"});
  },
  doApplyTicketFee() {},
  navigateToSub(ev) {
    let v = this;
    console.log('navigateToSub', ev);
    wx.navigateTo({ url: "/pages/rent/submit"});
  },

});